#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

echo "Installing dependencies"
"${DIRECTORY}/pkg-install" "kmod zip unzip" "$(dirname "$0")" || exit 1

echo "Creating directories"
mkdir -p ~/.minecraft ~/lwjgl3arm32 ~/lwjgl2arm32 ~/lunarassets
sudo mkdir -p /opt/jdk

#Download client and assorted java stuff
echo "Downloading lunarclient.AppImage"
errors="$(wget -O ~/lunarassets/lunarclient.AppImage 'https://github.com/gl91306/lunar/raw/master/lunarclient-2.8.0-armv7l.AppImage' 2>&1)" || error "Failed to download lunarclient.AppImage\nErrors:\n$errors"
chmod +x ~/lunarassets/lunarclient.AppImage || error "Failed to make lunarclient.AppImage executable!"

echo "Downloading lunarclient.png"
errors="$(wget -O ~/lunarassets/lunarclient.png https://github.com/gl91306/lunar/raw/master/lunarclient.png 2>&1)" || error "Failed to download lunarclient.png\nErrors:\n$errors"

echo "Downloading jdk-16.tar.gz"
sudo rm -rf /opt/jdk/jdk-16.0.1+9-jre ~/jdk-16.tar.gz
errors="$(wget -O ~/jdk-16.tar.gz 'https://github.com/gl91306/lunar/raw/master/jdk-16.0.1%2B9-jre.tar.gz' 2>&1)" || error "Failed to download jdk-16.tar.gz\nErrors:\n$errors"
echo "Extracting jdk-16.tar.gz to /opt/jdk"
sudo tar -zxf ~/jdk-16.tar.gz -C /opt/jdk || error "Failed to extract jdk-16.tar.gz"
rm -f ~/jdk-16.tar.gz
sudo mv /opt/jdk/opt/jdk/jdk-16.0.1+9-jre /opt/jdk
sudo rm -rf /opt/jdk/opt

echo "Setting up lwjgl3arm32 folder"
echo "- Downloading lwjgl3arm32.tar.gz"
errors="$(wget -O ~/lwjgl3arm32.tar.gz 'https://github.com/mikehooper/Minecraft/raw/main/lwjgl3arm32.tar.gz' 2>&1)" || error "Failed to download lwjgl3arm32.tar.gz\nErrors:\n$errors"
echo "- Extracting lwjgl3arm32.tar.gz"
tar -zxf ~/lwjgl3arm32.tar.gz -C ~/lwjgl3arm32 || error "Failed to extract lwjgl3arm32.tar.gz"
rm -f ~/lwjgl3arm32.tar.gz
echo "- libwebp-imageio32.so"
errors="$(wget -O ~/lwjgl3arm32/libwebp-imageio32.so 'https://github.com/gl91306/lunar/raw/master/libwebp-imageio32.so' 2>&1)" || error "Failed to download libwebp-imageio32.so\nErrors:\n$errors"
echo "- libgstreamer-lite.so"
errors="$(wget -O ~/lwjgl3arm32/libgstreamer-lite.so 'https://github.com/gl91306/lunar/raw/master/libgstreamer-lite.so' 2>&1)" || error "Failed to download libgstreamer-lite.so\nErrors:\n$errors"
echo "- libjfxmedia.so"
errors="$(wget -O ~/lwjgl3arm32/libjfxmedia.so 'https://github.com/gl91306/lunar/raw/master/libjfxmedia.so' 2>&1)" || error "Failed to download libjfxmedia.so\nErrors:\n$errors"

echo "Setting up lwjgl2arm32 folder"
echo "- Downloading lwjgl2arm32.tar.gz"
errors="$(wget -O ~/lwjgl2arm32.tar.gz 'https://github.com/mikehooper/Minecraft/raw/main/lwjgl2arm32.tar.gz' 2>&1)" || error "Failed to download lwjgl2arm32.tar.gz\nErrors:\n$errors"
echo "- Extracting lwjgl2arm32.tar.gz"
tar -zxf ~/lwjgl2arm32.tar.gz -C ~/lwjgl2arm32 || error "Failed to extract lwjgl2arm32.tar.gz"
rm -f ~/lwjgl2arm32.tar.gz
echo "- libwebp-imageio32.so"
cp ~/lwjgl3arm32/libwebp-imageio32.so ~/lwjgl2arm32
echo "- libgstreamer-lite.so"
cp ~/lwjgl3arm32/libgstreamer-lite.so ~/lwjgl2arm32
echo "- libjfxmedia.so"
cp ~/lwjgl3arm32/libjfxmedia.so ~/lwjgl2arm32
echo "- libopenal.so"
rm -f ~/lwjgl2arm32/libopenal.so
cp ~/lwjgl3arm32/libopenal.so ~/lwjgl2arm32

echo "Downloading javafx-sdk-17.zip"
errors="$(wget -O ~/javafx-sdk-17.zip 'https://github.com/gl91306/lunar/raw/master/javafx-sdk-17.zip' 2>&1)" || error "Failed to download javafx-sdk-17.zip\nErrors:\n$errors"
echo "Extracting javafx-sdk-17.zip"
unzip -oq ~/javafx-sdk-17.zip -d ~/lunarassets || error "Failed to extract javafx-sdk-17.zip"
rm -f ~/javafx-sdk-17.zip

echo "- libjfxwebkit.so"
errors="$(wget -O ~/lunarassets/javafx-sdk-17/lib/libjfxwebkit.so 'https://github.com/gl91306/lunar/raw/master/libjfxwebkit.so' 2>&1)" || error "Failed to download libjfxwebkit.so\nErrors:\n$errors"

#Then make menu shortcut
echo "[Desktop Entry]
Version=1.0
Type=Application
Name=Minecraft Launcher (Lunar)
Comment=3D block based sandbox game
Icon=$(dirname "$0")/icon-64.png
Exec=bash -c "\""sudo modprobe fuse ; JAVA_HOME=/opt/jdk/jdk1.8.0_251 PATH=/opt/jdk/jdk1.8.0_251/bin/:$PATH MESA_GL_VERSION_OVERRIDE=3.3 $HOME/lunarassets/lunarclient.AppImage"\""
StartupNotify=true
Categories=Game;" > ~/.local/share/applications/minecraftjava.desktop

echo "This fork of Lunar Client is made by the devs at Pi-Ware, an alternative to Pi-Apps with a focus on games and multi-os support (Arch and Manjaro).
You can find more about Pi-Ware at https://github.com/piware14/pi-ware
You should also contact them for support if a issue arises with the launcher.

To run: Menu -> Games -> Minecraft Launcher (Lunar)"













